Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Eta is a fast, lightweight, and highly configurable embedded JavaScript template engine. It is designed to be simple and easy to use, while also being powerful enough to handle complex templating needs.
Basic Template Rendering
This feature allows you to render a basic template with embedded JavaScript expressions. The `render` function takes a template string and a data object, and returns the rendered string.
const eta = require('eta');
const template = 'Hello, <%= it.name %>!';
const data = { name: 'World' };
const result = eta.render(template, data);
console.log(result); // Output: Hello, World!
Template Caching
Eta supports template caching, which allows you to define and reuse templates by name. This can improve performance by avoiding the need to recompile templates on each render.
const eta = require('eta');
eta.templates.define('greeting', 'Hello, <%= it.name %>!');
const data = { name: 'World' };
const result = eta.render('greeting', data);
console.log(result); // Output: Hello, World!
Custom Filters
Eta allows you to define custom filters that can be used within templates to transform data. In this example, a custom filter `shout` is defined to convert a string to uppercase and add exclamation marks.
const eta = require('eta');
eta.configure({
filters: {
shout: (str) => str.toUpperCase() + '!!!'
}
});
const template = 'Hello, <%= it.name | shout %>!';
const data = { name: 'World' };
const result = eta.render(template, data);
console.log(result); // Output: Hello, WORLD!!!
EJS (Embedded JavaScript) is a simple templating language that lets you generate HTML markup with plain JavaScript. It is similar to Eta in terms of syntax and functionality, but Eta is generally faster and more lightweight.
Pug (formerly known as Jade) is a high-performance template engine heavily influenced by Haml. It is known for its clean and minimal syntax. Compared to Eta, Pug offers a more concise syntax but has a steeper learning curve.
Handlebars is a popular templating engine that provides a more logic-less approach to templates. It uses a Mustache-like syntax and is known for its simplicity and ease of use. Unlike Eta, Handlebars enforces a separation of logic and presentation.
Documentation - Chat - RunKit Demo - Playground
Summary
Eta is a lightweight and blazing fast embedded JS templating engine that works inside Node, Deno, and the browser. Created by the developers of Squirrelly, it's written in TypeScript and emphasizes phenomenal performance, configurability, and low bundle size.
<%= someval + "string %>" %>
compiles correctly, while it fails with doT or EJSEta's syntax is very similar to EJS' (most templates should work with either engine), Eta has a similar API, and Eta and EJS share the same file-handling logic. Here are the differences between Eta and EJS:
-
), something that doesn't work in EJS because EJS uses -
on the left side to indicate that the value shouldn't be escaped. Instead, Eta uses ~
to output a raw value{{
and }}
, for example, while with EJS this isn't possible/* ... */
which allows commenting around template tags<%= "%>" %>
works in Eta, while it breaks in EJS<%=
to <%*
Eta and doT.js both allow embedded JavaScript, and both have best-in-class performance when compared to other template engines (though Eta is slightly faster with HTML-escaped templates). Here are some of the differences between Eta and doT.js:
<%= "%>" %>
works in Eta, while the equivalent breaks in doTEta and Handlebars are very different in some ways -- Eta is an embedded template engine, while Handlebars is a logic-less template engine. Here some additional differences between Eta and Handlebars:
<%=
to <%*
Template literals are a super useful tool, especially for shortening simple string concatenation. But writing complete templates using template literals can quickly get out of hand. Here's a comparison of Eta and template literals:
{{
and }}
, for example, or set them to ${
and }
to mimic template literals/* ... */
syntax, just like in regular JavaScript. Template literals require you to stick a blank string after the comment: /* ... */""
, which is much less readable? ... : ... ? ... : ...
. Writing conditionals in Eta is much simpler and more readableSimply put, Eta is super: super lightweight, super fast, super powerful, and super simple. Like with EJS, you don't have to worry about learning an entire new templating syntax. Just write JavaScript inside your templates.
"Eta" means tiny in Esperanto. Plus, it can be used as an acronym for all sorts of cool phrases: "ECMAScript Template Awesomeness", "Embedded Templating Alternative", etc....
Additionally, Eta is a letter of the Greek alphabet (it stands for all sorts of cool things in various mathematical fields, including efficiency) and is three letters long (perfect for a file extension).
@shadowtime2000 created eta-vscode.
eslint-plugin-eta was created to provide an ESLint processor so you can lint your Eta templates.
An official Eta CLI exists called etajs-cli.
Currently there is no official Webpack integration but @clshortfuse shared the loader he uses:
{
loader: 'html-loader',
options: {
preprocessor(content, loaderContext) {
return eta.render(content, {}, { filename: loaderContext.resourcePath });
},
},
}
To operate with Eta templates in Node-RED: @ralphwetzel/node-red-contrib-eta
We know nobody reads through the long and boring documentation in the ReadMe anyway, so head over to the documentation website:
import * as Eta from "eta";
var myTemplate = "<p>My favorite kind of cake is: <%= it.favoriteCake %></p>";
Eta.render(myTemplate, { favoriteCake: "Chocolate!" });
// Returns: '<p>My favorite kind of cake is: Chocolate!</p>'
<% if(it.somevalue === 1) { %>
Display this
<% } else { %>
Display this instead
<% } %>
<ul>
<% it.users.forEach(function(user){ %>
<li><%= user.name %></li>
<% }) %>
</ul>
<%~ include('mypartial') %>
<%~ includeFile('./footer') %>
<%~ include('users', {users: it.users}) %>
Tests can be run with npm test
. Multiple tests check that parsing, rendering, and compiling return expected results, formatting follows guidelines, and code coverage is at the expected level.
To be added
eta
Made with ❤ by @nebrelbug and all these wonderful contributors (emoji key):
Ben Gubler 💻 💬 📖 ⚠️ | Clite Tailor 🤔 💻 | Ioan CHIRIAC 💻 🤔 | Craig Morten 💻 | Rajan Tiwari 💡 | shadowtime2000 💻 🤔 ⚠️ | Hamza Hamidi 📖 |
Calum Knott 🤔 | nhaef 💻 | Gün 💻 |
This project follows the all-contributors specification. Contributions of any kind are welcome!
FAQs
Lightweight, fast, and powerful embedded JS template engine
We found that eta demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.